package zifuchuan;

public class _09去掉字符串中连续出现K次的0 {
    public static void main(String[] args) {
        String s = "100000123000";
        int k = 2;
        String res = deleteKZero(s, k);
        String result = remove(s,k);
        System.out.println(res);
        System.out.println(result);
    }


    public static String deleteKZero(String initString,int k){
        StringBuilder sb = new StringBuilder();
        int count = 0;
        char[] chars = initString.toCharArray();
        for (int i = 0 ; i < chars.length;i++){
            char c = chars[i];
            if (c == '0'){
                count ++;
            }else {
                for (int j = 0 ; j < count % k ;j++){
                    sb.append('0');
                }
                sb.append(c);
                count = 0;
            }
        }
        //处理最后出现的0
        for (int i = 0; i < count%k;i++){
            sb.append('0');
        }
        return sb.toString();
    }

    //正则表达式解法   0{k} 代表连续出现k次的0
    public static String remove(String initString , int k){
        String regexp = "0{" + k + "}";
        return initString.replaceAll(regexp,"");
    }
}
